home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 June: Reference Library / Dev.CD Jun 96 RL / Dev.CD Jun 96 RL.toast / What's New? / Development Kits / Apple Game Sprockets DR1 / Examples / SoundSprocketTest / TS3Utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-24  |  11.0 KB  |  460 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        TS3Utils.c
  3.  *    Author:        Dan Venolia
  4.  *
  5.  *    Contents:    Some utilities.
  6.  *
  7.  *    Copyright © 1996 Apple Computer, Inc.
  8.  */
  9.  
  10. #include <assert.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. #include "TS3Utils.h"
  16.  
  17.  
  18. static UserItemUPP            gUtilsOKUserItemProc    = NULL;
  19.  
  20.  
  21. static pascal void Utils_OKUserItem(
  22.     DialogPtr            inDialog,
  23.     short                inItem);
  24.  
  25.  
  26. /* =============================================================================
  27.  *        Utils_Init (external)
  28.  *
  29.  *    Initializes the utilities.
  30.  * ========================================================================== */
  31. void Utils_Init(
  32.     void)
  33. {
  34. }
  35.  
  36.  
  37. /* =============================================================================
  38.  *        Utils_Exit (external)
  39.  *
  40.  *    Prepares for exit.
  41.  * ========================================================================== */
  42. void Utils_Exit(
  43.     void)
  44. {
  45.     if (gUtilsOKUserItemProc != NULL)
  46.     {
  47.         DisposeRoutineDescriptor(gUtilsOKUserItemProc);
  48.         gUtilsOKUserItemProc = NULL;
  49.     }
  50. }
  51.  
  52.  
  53. /* =============================================================================
  54.  *        Utils_Interval (external)
  55.  *
  56.  *    Returns the interval, in seconds, between two results of Microseconds.
  57.  * ========================================================================== */
  58. float Utils_Interval(
  59.     const UnsignedWide*    inPrevTime,
  60.     const UnsignedWide*    inCurrTime)
  61. {
  62.     //• Should actually take high words into consideration too
  63.     return 0.000001*(inCurrTime->lo - inPrevTime->lo);
  64. }
  65.  
  66.  
  67. /* =============================================================================
  68.  *        Utils_OKUserItem (internal)
  69.  *
  70.  *    Draws the user item used for framing the OK button.  The user item should
  71.  *    be four pixels larger than the OK button in each direction.
  72.  * ========================================================================== */
  73. pascal void Utils_OKUserItem(
  74.     DialogPtr            inDialog,
  75.     short                inItem)
  76. {
  77.     short                itemType;
  78.     Handle                itemHandle;
  79.     Rect                itemBounds;
  80.     
  81.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  82.     
  83.     PenSize(3, 3);
  84.     FrameRoundRect(&itemBounds, 16, 16);
  85.     PenSize(1, 1);
  86. }
  87.  
  88.  
  89. /* =============================================================================
  90.  *        Utils_GetOKUserItemProc (external)
  91.  *
  92.  *    Returns the UPP for the dialog user item that outlines the default button.
  93.  * ========================================================================== */
  94. UserItemUPP Utils_GetOKUserItemProc(
  95.     void)
  96. {
  97.     if (gUtilsOKUserItemProc == NULL)
  98.     {
  99.         gUtilsOKUserItemProc = NewUserItemProc(Utils_OKUserItem);
  100.         assert(gUtilsOKUserItemProc != NULL);
  101.     }
  102.     
  103.     return gUtilsOKUserItemProc;
  104. }
  105.  
  106.  
  107. /* =============================================================================
  108.  *        Utils_SetStr255Field (external)
  109.  *
  110.  *    Changes the text of the given dialog item to the given string value.  If
  111.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  112.  *    of course be editable or static text.
  113.  * ========================================================================== */
  114. void Utils_SetStr255Field(
  115.     DialogPtr            inDialog,
  116.     short                inItem,
  117.     ConstStr255Param    inValue,
  118.     Boolean                inHasValue)
  119. {
  120.     short                itemType;
  121.     Handle                itemHandle;
  122.     Rect                itemBounds;
  123.     
  124.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  125.     if (inHasValue)
  126.     {
  127.         SetDialogItemText(itemHandle, inValue);
  128.     }
  129.     else
  130.     {
  131.         SetDialogItemText(itemHandle, "\p");
  132.     }
  133. }
  134.  
  135.  
  136. /* =============================================================================
  137.  *        Utils_GetStr255Field (external)
  138.  *
  139.  *    Interprets the value of the given editable or static text field as an
  140.  *    string value.  If outHasValue is NULL then the field must not be blank.
  141.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  142.  *    and true if it has a value.  Returns false if there is a formatting problem.
  143.  * ========================================================================== */
  144. Boolean Utils_GetStr255Field(
  145.     DialogPtr            inDialog,
  146.     short                inItem,
  147.     Str255                outValue,
  148.     Boolean*            outHasValue)
  149. {
  150.     short                itemType;
  151.     Handle                itemHandle;
  152.     Rect                itemBounds;
  153.     
  154.     // Grab the string
  155.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  156.     GetDialogItemText(itemHandle, outValue);
  157.     
  158.     if (outHasValue != NULL)
  159.     {
  160.         *outHasValue = outValue[0] > 0;
  161.     }
  162.     
  163.     return true;
  164. }
  165.  
  166.  
  167. /* =============================================================================
  168.  *        Utils_SetUInt32Field (external)
  169.  *
  170.  *    Changes the text of the given dialog item to the given integer value.  If
  171.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  172.  *    of course be editable or static text.
  173.  * ========================================================================== */
  174. void Utils_SetUInt32Field(
  175.     DialogPtr            inDialog,
  176.     short                inItem,
  177.     UInt32                inValue,
  178.     Boolean                inHasValue)
  179. {
  180.     Str255                str;
  181.     short                itemType;
  182.     Handle                itemHandle;
  183.     Rect                itemBounds;
  184.     
  185.     if (inHasValue)
  186.     {
  187.         sprintf((char*) str, "x%lu", inValue);
  188.         str[0] = strlen((char*) str) - 1;
  189.     }
  190.     else
  191.     {
  192.         str[0] = 0;
  193.     }
  194.     
  195.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  196.     SetDialogItemText(itemHandle, str);
  197. }
  198.  
  199.  
  200. /* =============================================================================
  201.  *        Utils_GetUInt32Field (external)
  202.  *
  203.  *    Interprets the value of the given editable or static text field as an
  204.  *    integer value.  If outHasValue is NULL then the field must not be blank.
  205.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  206.  *    and true if it has a value.  Returns false if there is a formatting problem.
  207.  * ========================================================================== */
  208. Boolean Utils_GetUInt32Field(
  209.     DialogPtr            inDialog,
  210.     short                inItem,
  211.     UInt32*                outValue,
  212.     Boolean*            outHasValue,
  213.     UInt32                inMin,
  214.     UInt32                inMax)
  215. {
  216.     Str255                str;
  217.     short                itemType;
  218.     Handle                itemHandle;
  219.     Rect                itemBounds;
  220.     
  221.     // Grab the string
  222.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  223.     GetDialogItemText(itemHandle, str);
  224.     str[str[0]+1] = 0;
  225.     
  226.     // Parse it
  227.     if (sscanf((char*) str, "%*c%lu", outValue) > 0)
  228.     {
  229.         // Got a number
  230.         if (outHasValue != NULL)
  231.         {
  232.             *outHasValue = true;
  233.         }
  234.         
  235.         // Check the range
  236.         if (*outValue < inMin || *outValue > inMax)
  237.         {
  238.             return false;
  239.         }
  240.     }
  241.     else
  242.     {
  243.         if (outHasValue != NULL)
  244.         {
  245.             *outHasValue = false;
  246.         }
  247.         else
  248.         {
  249.             return false;
  250.         }
  251.     }
  252.     
  253.     return true;
  254. }
  255.  
  256.  
  257. /* =============================================================================
  258.  *        Utils_SetFloatField (external)
  259.  *
  260.  *    Changes the text of the given dialog item to the given float value.  If
  261.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  262.  *    of course be editable or static text.
  263.  * ========================================================================== */
  264. void Utils_SetFloatField(
  265.     DialogPtr            inDialog,
  266.     short                inItem,
  267.     float                inValue,
  268.     Boolean                inHasValue)
  269. {
  270.     Str255                str;
  271.     short                itemType;
  272.     Handle                itemHandle;
  273.     Rect                itemBounds;
  274.     float                abs;
  275.     float                fract;
  276.     
  277.     if (inHasValue)
  278.     {
  279.         abs = fabsf(inValue);
  280.         fract = abs-floorf(abs);
  281.         if (fract < 0.0001 || fract > 0.9999)
  282.         {
  283.             // Value is close enough to integer to show it that way
  284.             sprintf((char*) str, "x%.0f", inValue);
  285.         }
  286.         else
  287.         {
  288.             // Has a fractional part
  289.             sprintf((char*) str, "x%.2f", inValue);
  290.         }
  291.         str[0] = strlen((char*) str) - 1;
  292.     }
  293.     else
  294.     {
  295.         str[0] = 0;
  296.     }
  297.     
  298.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  299.     SetDialogItemText(itemHandle, str);
  300. }
  301.  
  302.  
  303. /* =============================================================================
  304.  *        Utils_GetFloatField (external)
  305.  *
  306.  *    Interprets the value of the given editable or static text field as an
  307.  *    float value.  If outHasValue is NULL then the field must not be blank.
  308.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  309.  *    and true if it has a value.  Returns false if there is a formatting problem.
  310.  * ========================================================================== */
  311. Boolean Utils_GetFloatField(
  312.     DialogPtr            inDialog,
  313.     short                inItem,
  314.     float*                outValue,
  315.     Boolean*            outHasValue,
  316.     float                inMin,
  317.     float                inMax)
  318. {
  319.     Str255                str;
  320.     short                itemType;
  321.     Handle                itemHandle;
  322.     Rect                itemBounds;
  323.     
  324.     // Grab the string
  325.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  326.     GetDialogItemText(itemHandle, str);
  327.     str[str[0]+1] = 0;
  328.     
  329.     // Parse it
  330.     if (sscanf((char*) str, "%*c%f", outValue) > 0)
  331.     {
  332.         // Got a number
  333.         if (outHasValue != NULL)
  334.         {
  335.             *outHasValue = true;
  336.         }
  337.         
  338.         // Check the range
  339.         if (*outValue < inMin || *outValue > inMax)
  340.         {
  341.             return false;
  342.         }
  343.     }
  344.     else
  345.     {
  346.         if (outHasValue != NULL)
  347.         {
  348.             *outHasValue = false;
  349.         }
  350.         else
  351.         {
  352.             return false;
  353.         }
  354.     }
  355.     
  356.     return true;
  357. }
  358.  
  359.  
  360. /* =============================================================================
  361.  *        Utils_SetVector3DField (external)
  362.  *
  363.  *    Changes the text of the given dialog item to the given TQ3ector3D value.  If
  364.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  365.  *    of course be editable or static text.
  366.  * ========================================================================== */
  367. void Utils_SetVector3DField(
  368.     DialogPtr            inDialog,
  369.     short                inItem,
  370.     const TQ3Vector3D*    inValue,
  371.     Boolean                inHasValue)
  372. {
  373.     Str255                str;
  374.     short                itemType;
  375.     Handle                itemHandle;
  376.     Rect                itemBounds;
  377.     float                abs;
  378.     float                fract;
  379.     UInt32                xPrecision;
  380.     UInt32                yPrecision;
  381.     UInt32                zPrecision;
  382.     
  383.     if (inHasValue)
  384.     {
  385.         abs = fabsf(inValue->x);
  386.         fract = abs-floorf(abs);
  387.         xPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  388.         
  389.         abs = fabsf(inValue->y);
  390.         fract = abs-floorf(abs);
  391.         yPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  392.         
  393.         abs = fabsf(inValue->z);
  394.         fract = abs-floorf(abs);
  395.         zPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  396.         
  397.         sprintf((char*) str, "x%.*f %.*f %.*f", xPrecision, inValue->x, yPrecision, inValue->y, zPrecision, inValue->z);
  398.         str[0] = strlen((char*) str) - 1;
  399.     }
  400.     else
  401.     {
  402.         str[0] = 0;
  403.     }
  404.     
  405.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  406.     SetDialogItemText(itemHandle, str);
  407. }
  408.  
  409.  
  410. /* =============================================================================
  411.  *        Utils_GetVector3DField (external)
  412.  *
  413.  *    Interprets the value of the given editable or static text field as an
  414.  *    TQ3Vector3D value.  If outHasValue is NULL then the field must not be blank.
  415.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  416.  *    and true if it has a value.  Returns false if there is a formatting problem.
  417.  * ========================================================================== */
  418. Boolean Utils_GetVector3DField(
  419.     DialogPtr            inDialog,
  420.     short                inItem,
  421.     TQ3Vector3D*        outValue,
  422.     Boolean*            outHasValue)
  423. {
  424.     Str255                str;
  425.     short                itemType;
  426.     Handle                itemHandle;
  427.     Rect                itemBounds;
  428.     
  429.     // Grab the string
  430.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  431.     GetDialogItemText(itemHandle, str);
  432.     str[str[0]+1] = 0;
  433.     
  434.     // Parse it
  435.     if (sscanf((char*) str, "%*c%f%f%f", &outValue->x, &outValue->y, &outValue->z) == 3)
  436.     {
  437.         // Got a number
  438.         if (outHasValue != NULL)
  439.         {
  440.             *outHasValue = true;
  441.         }
  442.     }
  443.     else
  444.     {
  445.         if (outHasValue != NULL)
  446.         {
  447.             *outHasValue = false;
  448.         }
  449.         else
  450.         {
  451.             return false;
  452.         }
  453.     }
  454.     
  455.     return true;
  456. }
  457.  
  458.  
  459.  
  460.